Skip to content

Conversation

@sobolevn
Copy link
Member

Closes #13551

Basically, this is a wrapper around

name: str

>>> from aiofiles.threadpool import stdin
>>> stdin
<aiofiles.threadpool.text.AsyncTextIndirectIOWrapper object at 0x105cb6d90> wrapping <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>
>>> stdin.name
'<stdin>'

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

def newlines(self) -> str | tuple[str, ...] | None: ...
@property
def name(self) -> FileDescriptorOrPath: ...
def name(self) -> str: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do this unconditionally. aiofiles.threadpool.open() will also return an AsyncTextIOWrapper, which might be a file descriptor:

import asyncio
from os import fdopen

from aiofiles import open as aioopen

async def main():
    f = open("bar.txt")
    async with aioopen(f.fileno()) as aiof:
        print(type(aiof.name))  # <class 'int'>

asyncio.run(main())

Maybe it's best if we make _UnknownAsyncTextIO and the two wrapper classes generic over name with a default of FileDescriptorOrPath. Then we can bind this to str in the return type of NamedTemporaryFile.

In fact, we could use this as a template for the stdlib, where we already have a comment alluding to this:

typeshed/stdlib/typing.pyi

Lines 771 to 774 in 0b8dcfc

# Usually str, but may be bytes if a bytes path was passed to open(). See #10737.
# If PEP 696 becomes available, we may want to use a defaulted TypeVar here.
@property
def name(self) -> str | Any: ...

Copy link
Member Author

@sobolevn sobolevn Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Thanks a lot! ❤️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, when I double checked that, I think that I would prefer to close this PR, because it is too complex. Notice that def NamedTemporaryFile has multiple @overloads that return different subtypes of _UnknownAsyncBinaryIO, not even direct ones. I think that making all of the generic is an overkill :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[aiofiles] aiofiles.tempfile.NamedTemporaryFile.name should probably be a str

2 participants